package com.dyh.hissystem.controller;

import com.dyh.hissystem.dto.User;
import com.dyh.hissystem.service.UserService;
import com.dyh.hissystem.util.RespUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @date 2025/8/13
 */

@CrossOrigin
@RestController
@Log4j2
public class UserController {
    private final UserService userService;
    public UserController(UserService userService){ this.userService=userService;}

    @GetMapping("findAllUser")
    public RespUtil findAllUser(){
        List<User> userList;
        userList=userService.list();
        return RespUtil.success(userList);
    }

    @PostMapping("addUser")
    public RespUtil addUser(@RequestBody Map<String,Object> userData){
        User user = new User((String)userData.get("userName"),(String)userData.get("name"),
                (int)userData.get("sex"),(String)userData.get("password"),null,"0","1",null,null);
        Boolean flag = userService.save(user);
        if(Boolean.TRUE.equals(flag)){
            return RespUtil.success();
        }else{
            return RespUtil.error();
        }
    }

    @PostMapping("/loopAddUser")
    public RespUtil loopAddUser(@RequestBody Map<String,Object> batchUser){
        User user = new User((String)batchUser.get("userName"),(String)batchUser.get("name"),
                (int)batchUser.get("sex"),(String)batchUser.get("password"),null,"0","1",null,null);
        Boolean flag = userService.save(user);
        if(Boolean.TRUE.equals(flag)){
            return RespUtil.success(true);
        }
        return RespUtil.error(false);
    }
}
